<script type="text/javascript">
/*----------------------------------------------------------
 EVENT
-----------------------------------------------------------*/
//create
$(document).on("click", "#createContent", function(){
  var url = "<?php echo Yii::app()->createUrl('dadmin/menu/create/parentId'); ?>/"
    + $('#parentId').val();
  loadFrame(url, '#dialogForm', 500, 340, refreshMenu, 'frameForm', '<?php echo Yii::t('DadminModule.app', 'add') ?>');
});

//update
$(document).on("click", ".updateContent", function(){
  var url = $(this).attr('href');
  loadFrame(url, '#dialogForm', 500, 340, refreshMenu, 'frameForm', '<?php echo Yii::t('DadminModule.app', 'update') ?>');
  
  return false;
});

//delete
$(document).on("click", ".deleteMenu", function(){
    var params = { msg: '<?php echo Yii::t('DadminModule.msg', 'delete_confirm') ?>' };
    var url = $(this).attr('href');
    
    $('#null').showConfirm(params, function(){
        $.getJSON(url, '', function(result) {
            if ( result.success === true ) {
                refreshMenu();
                showMessage('#msgSuccess', result.message);
            } else {
                showMessage('#msgError', result.message);
            }
        });
    }, '');
    
    return false;
});

$('#dialogForm').bind('openDialogLinks', function() {
  var urlStaticContent = '<?php echo $this->createUrl('newsletterContent/assignLink/type_id/default') ?>';
  var urlCategory = '<?php echo $this->createUrl('newsletterType/assignLink') ?>';
  var urlCfGocAnh = '<?php echo $this->createUrl('cfGocAnh/assignLink') ?>';

  var opt = $(this).data('option');
  var url = urlStaticContent;
  
  switch (opt) {
      case 1: url = urlStaticContent; break;
      case 2: url = urlCategory; break;
      case 3: url = urlCfGocAnh; break;
  }
  
  loadFrame(url, '#dialogLinks', 700, 450, '', 'frameLinks', '<?php echo Yii::t('DadminModule.app', 'link_helper') ?>');
});

$('#dialogLinks').bind('assignLink', function() {
  $('#frameForm').contents()
    .find('#DMenu_link')
    .val($(this).data('link'));
});

/*----------------------------------------------------------
PROCESSING
 ----------------------------------------------------------*/
function loadFrame(url, container, width, height, callBack, frameId, title) {
  var frameAttr = 'width="100%" height="100%"'
    + 'marginWidth="0" marginHeight="0"'
    + 'frameBorder="0"';
  
  myDialog = $(container).html('<iframe id="' + frameId + '" '+ frameAttr + '/>').dialog({
    modal: true,
    width: width, height: height,
    position: ['center', 55],
    draggable: false, resizable: false,
    bgiframe: true,
    close: callBack,
    title: title,
    zIndex: 10000
  });
  $("#" + frameId).attr("src", url);

  return myDialog;
};

function showConfirm()
{
  return confirm("<?php echo Yii::t('DadminModule.msg', 'delete_confirm') ?>");
}

function refreshMenu()
{
  loadMenu();
  loadChildMenu();
}

function loadMenu()
{
  var url = "<?php echo $this->createUrl('menu/menuShowFull'); ?>";
  $('#typeTree').load(url);
}

function loadChildMenu()
{
  var parentId = $('#parentId').val();
  $('#typeTree .link').each(function(){
    if ($(this).attr('ref') == parentId)
      $(this).trigger('click');
  })
}
</script>